ÄÚǶGridView´øÐÞ¸Ä Ç°¶Ë´úÂë ¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡<%#Eval("ID") %> ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡¡¡¡¡¡¡ ¡¡¡¡¡¡
ºǫ́´úÂë DataAccess da; ¡¡¡¡protected void Page_Load(object sender, EventArgs e) ¡¡¡¡{ ¡¡¡¡¡¡¡¡if (Session["da"] == null) ¡¡¡¡¡¡¡¡{ ¡¡¡¡¡¡¡¡¡¡¡¡da = new DataAccess(); ¡¡¡¡¡¡¡¡¡¡¡¡Session["da"] = da; ¡¡¡¡¡¡¡¡} ¡¡¡¡¡¡¡¡else ¡¡¡¡¡¡¡¡{ ¡¡¡¡¡¡¡¡¡¡¡¡da = Session["da"] as DataAccess; ¡¡¡¡¡¡¡¡} ¡¡¡¡¡¡¡¡if (!IsPostBack) ¡¡¡¡¡¡¡¡{ ¡¡¡¡¡¡¡¡¡¡¡¡Bind(); ¡¡¡¡¡¡¡¡} ¡¡¡¡} ¡¡¡¡ ¡¡¡¡private void Bind() ¡¡¡¡{ ¡¡¡¡¡¡¡¡ListView1.DataSource = da.List; ¡¡¡¡¡¡¡¡ListView1.DataBind(); ¡¡¡¡} ¡¡¡¡protected void ListView1_SelectedIndexChanging(object sender, ListViewSelectEventArgs e) ¡¡¡¡{ ¡¡¡¡¡¡¡¡ListView1.SelectedIndex = e.NewSelectedIndex; ¡¡¡¡¡¡¡¡Bind(); ¡¡¡¡¡¡¡¡ViewState["SelectedIndex"] = e.NewSelectedIndex; ¡¡¡¡¡¡¡¡int selectIndex = (int)ViewState["SelectedIndex"]; ¡¡¡¡¡¡¡¡GridView gv = (GridView)ListView1.Items[selectIndex].FindControl("GridView1"); ¡¡¡¡¡¡¡¡gv.DataSource = da.List; ¡¡¡¡¡¡¡¡gv.DataBind(); ¡¡¡¡} ¡¡¡¡protected void GridView1_Editing(object sender, GridViewEditEventArgs e) ¡¡¡¡{ ¡¡¡¡¡¡¡¡int selectIndex = (int)ViewState["SelectedIndex"]; ¡¡¡¡¡¡¡¡GridView gv = (GridView)ListView1.Items[selectIndex].FindControl("GridView1"); ¡¡¡¡¡¡¡¡gv.EditIndex = e.NewEditIndex; ¡¡¡¡¡¡¡¡gv.DataSource = da.List; ¡¡¡¡¡¡¡¡gv.DataBind(); ¡¡¡¡} ¡¡¡¡protected void GridView1_Updating(object sender, GridViewUpdateEventArgs e) ¡¡¡¡{ ¡¡¡¡¡¡¡¡int selectIndex = (int)ViewState["SelectedIndex"]; ¡¡¡¡¡¡¡¡GridView gv = (GridView)ListView1.Items[selectIndex].FindControl("GridView1"); ¡¡¡¡¡¡¡¡string id=gv.Rows[e.RowIndex].Cells[1].Text; ¡¡¡¡¡¡¡¡string name= ((TextBox)gv.Rows[e.RowIndex].Cells[2].Controls[0]).Text; ¡¡¡¡¡¡¡¡string Age= ((TextBox)gv.Rows[e.RowIndex].Cells[3].Controls[0]).Text; ¡¡¡¡¡¡¡¡Employee employee = da.Find(Convert.ToInt32(id)); ¡¡¡¡¡¡¡¡employee.Name = name; ¡¡¡¡¡¡¡¡employee.Age = Convert.ToInt32(Age); ¡¡¡¡¡¡¡¡gv.EditIndex = -1; ¡¡¡¡¡¡¡¡gv.DataSource = da.List; ¡¡¡¡¡¡¡¡gv.DataBind(); ¡¡¡¡} ¡¡¡¡protected void GridView1_CancelingEdit(object sender, GridViewCancelEditEventArgs e) ¡¡¡¡{ ¡¡¡¡¡¡¡¡int selectIndex = (int)ViewState["SelectedIndex"]; ¡¡¡¡¡¡¡¡GridView gv = (GridView)ListView1.Items[selectIndex].FindControl("GridView1"); ¡¡¡¡¡¡¡¡gv.EditIndex = -1; ¡¡¡¡¡¡¡¡gv.DataSource = da.List; ¡¡¡¡¡¡¡¡gv.DataBind(); ¡¡¡¡} Ä£ÄâÊý¾ÝÌṩºÍǰ±ßÒ»ÑùÕâÀï¾Í²»ÌùÁË Ä£ÄâÊý¾ÝÌṩ´úÂ룺 public class DataAccess { ¡¡¡¡public List List; ¡¡¡¡public DataAccess() ¡¡¡¡{ ¡¡¡¡¡¡¡¡List = new List(); ¡¡¡¡¡¡¡¡List = new List(); ¡¡¡¡¡¡¡¡Employee e1 = new Employee { ID = 1, Name = "A", Age = 10, Sex = 1 }; ¡¡¡¡¡¡¡¡Employee e2 = new Employee { ID = 3, Name = "M", Age = 30, Sex = 0 }; ¡¡¡¡¡¡¡¡Employee e3 = new Employee { ID = 2, Name = "B", Age = 40, Sex = 1 }; ¡¡¡¡¡¡¡¡Employee e4 = new Employee { ID = 4, Name = "S", Age = 19, Sex = 0 }; ¡¡¡¡¡¡¡¡Employee e5 = new Employee { ID = 6, Name = "X", Age = 18, Sex = 1 }; ¡¡¡¡¡¡¡¡Employee e6 = new Employee { ID = 5, Name = "W", Age = 20, Sex = 0 }; ¡¡¡¡¡¡¡¡List.Add(e1); ¡¡¡¡¡¡¡¡List.Add(e2); ¡¡¡¡¡¡¡¡List.Add(e3); ¡¡¡¡¡¡¡¡List.Add(e4); ¡¡¡¡¡¡¡¡List.Add(e5); ¡¡¡¡¡¡¡¡List.Add(e6); ¡¡¡¡ ¡¡¡¡} ¡¡¡¡public List GetEmployee(int startIndex, int rowsCount) ¡¡¡¡{ ¡¡¡¡¡¡¡¡List listTemp = new List(); ¡¡¡¡¡¡¡¡for (int i = startIndex; i < startIndex+rowsCount; i++) ¡¡¡¡¡¡¡¡{ ¡¡¡¡¡¡¡¡¡¡¡¡listTemp.Add(List[i-1]); ¡¡¡¡¡¡¡¡} ¡¡¡¡¡¡¡¡return listTemp; ¡¡¡¡} }